using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.DOWNLOAD.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            btnDelete.Visible = this.ItemID > 0;

            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            var item = new Components.Item();
            if (this.ItemID > 0)
            {
                item = Components.Item.Load(this.ItemID);
            }

            item.CategoryID = cbCategory.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbCategory.SelectedValue);
            item.Title = txtTitle.Text;
            item.SubTitle = txtSubTitle.Text;            
            item.CoverImageUrl = txtImageUrl.Text;
            item.Keywords = txtKeywords.Text;
            item.Description = Server.HtmlEncode(txtDescription.Value);
            item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;

            if (this.ItemID == 0)
            {
                item.CreatedDate = DateTime.Now;
                this.ItemID = item.Insert();
            }
            else
            {
                item.ID = Convert.ToInt32(Request.QueryString["itemID"]);
                item.Update();
            }

            // Go to upload file page.
            string url = HttpUrlBuilder.BuildShareUrl(this.ModuleID, this.ItemID, "UploadFile");
            Response.Redirect(url);

        }  

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {                
                Category.BuildDropDownListControl(ref cbCategory, this.ModuleID);

                if (this.ItemID > 0)
                {
                    var item = Components.Item.Load(this.ItemID);
                    if (item != null)
                    {
                        cbCategory.SelectedValue = item.CategoryID.ToString();
                        txtTitle.Text = item.Title;
                        txtSubTitle.Text = item.SubTitle;
                        txtImageUrl.Text = item.CoverImageUrl;
                        txtDescription.Value = Server.HtmlDecode(item.Description);
                        txtKeywords.Text = item.Keywords;
                    }
                }
            }
        }
    }
}